﻿using ComponentFactory.Krypton.Toolkit;
using SRB.Tg.Model;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Teigha.DatabaseServices;

namespace SRB.Tg.Cad
{
    public partial class PlinthInfoForm : KryptonForm
    {
        DataRow _row;
        double _scale;
        private static PlinthInfoForm _instance;
        public static PlinthInfoForm getInstance(DataRow row, double scale)
        {
            if (_instance == null)
            {
                _instance = new PlinthInfoForm(row,scale);
            }
            return _instance;
        }

        public PlinthInfoForm()
        {
            InitializeComponent();
        }
        public PlinthInfoForm(DataRow row, double scale)
        {
            InitializeComponent();
            _row = row;
            _scale = scale;
            this.configure_name.Text = row["configure_name"].ToString();
            this.reinforcement_net_spacing.Text = row["reinforcement_net_spacing"].ToString();
            this.reinforcement_net_diameter.Text = row["reinforcement_net_diameter"].ToString();
            this.reinforcement_net_layers_number.Text = row["reinforcement_net_layers_number"].ToString();     
            this.concrete_no.Text = row["concrete_no"].ToString();
            this.cross_section.Text = row["cross_section"].ToString();
            this.concrete_protect_layer.Text = row["concrete_protect_layer"].ToString();
            this.connector_spacing.Text = row["connector_spacing"].ToString();
            this.main_l_reinforcement_diameter.Text = row["main_l_reinforcement_diameter"].ToString();
            this.main_l_reinforcement_spacing.Text = row["main_l_reinforcement_spacing"].ToString();
            this.main_w_reinforcement_diameter.Text = row["main_w_reinforcement_diameter"].ToString();
            this.main_w_reinforcement_spacing.Text = row["main_w_reinforcement_spacing"].ToString();         
            this.length.Text = row["length"].ToString();
            this.width.Text = row["width"].ToString();
            this.height.Text = row["height"].ToString();
            this.reinforcement_grade.Text = row["reinforcement_grade"].ToString();
            this.structure_number.Text = row["structure_number"].ToString();
            this.structure_type.Text = row["structure_type"].ToString();
           
        }

        public void SavModify()
        {
            if (_row != null)
            {
                if (!_row.IsNull("id"))
                {
                    string updatesql = string.Format("update t_cushion_stone set main_l_reinforcement_diameter='{1}',"
                        + "main_l_reinforcement_spacing='{2}',main_w_reinforcement_diameter='{3}',main_w_reinforcement_spacing='{4}',reinforcement_net_diameter='{5}',"
                        + "reinforcement_net_spacing='{6}',reinforcement_net_layers_number='{7}',reinforcement_grade='{8}',concrete_no='{9}',"
                        + "concrete_protect_layer='{10}',connector_spacing='{11}',height='{12}',length='{13}',width='{14}'"
                        + " where id='{0}'", _row["id"].ToString(),
                  
                       this.main_l_reinforcement_diameter.Text, this.main_l_reinforcement_spacing.Text, this.main_w_reinforcement_diameter.Text,
                       this.main_w_reinforcement_spacing.Text, this.reinforcement_net_diameter.Text, this.reinforcement_net_spacing.Text,
                       this.reinforcement_net_layers_number.Text, this.reinforcement_grade.Text,
                       this.concrete_no.Text, this.concrete_protect_layer.Text, this.connector_spacing.Text,               
                       this.height.Text,this.length.Text, this.width.Text);

                    ////暂不更新，算量计算后期和服务端关联
                    MySqlHelper.ExecuteNonQuery(updatesql, CommandType.Text);

                    PlinthParameter para = ModelOper.GetPlinthPara(_row["id"].ToString(), out _row);
                    // 钢筋明细
                    string selectString = string.Format("SELECT * FROM t_reinforcement_summary where part_type='{0}' and parent_id='{1}' and status=0  order by no asc", 3, _row["id"].ToString());
                    System.Data.DataTable dt1 = MySqlHelper.ExecuteDataTable(selectString, System.Data.CommandType.Text);

                    // 材料明细
                    selectString = string.Format("SELECT * FROM t_material_summary where part_type='{0}' and parent_id='{1}' and calculate_type='{2}' and status=0 ", 3, _row["id"].ToString(), 1);
                    System.Data.DataTable dt2 = MySqlHelper.ExecuteDataTable(selectString, System.Data.CommandType.Text);
                    int OverAllScale = (int)_scale;

                    Plinth P1 = new Plinth(para);
                    Database db = HostApplicationServices.WorkingDatabase;
                    P1.DrawingPlinth(db, OverAllScale, dt1, dt2);
                }
            }
        }
    }
}
